<template>
  <div
    style="
      background-color: white;
      border: 1px solid grey;
      border-radius: 5px;
      height: 600px;
    "
  >
    <el-form
      class="user-account-key"
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item style="margin-left: 0px">
        <h1>修改密码</h1>
      </el-form-item>
      <el-divider style="width: 50%"></el-divider>
      <el-form-item label="用户名" prop="enterpriseAccountNumber" class="in0">
        <el-input
          type="text"
          v-model="form.enterpriseAccountNumber"
          autocomplete="off"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="原密码" prop="oldPassword" class="in0">
        <el-input
          type="password"
          placeholder="请输入原密码"
          v-model="form.oldPassword"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword" class="in0">
        <el-input
          type="password"
          placeholder="请设置新密码"
          v-model="form.newPassword"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newPassword2" class="in0">
        <el-input
          type="password"
          placeholder="请确认新密码"
          v-model="form.newPassword2"
        ></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 35%">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="$refs['form'].resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Qs from 'qs'
export default {
  data() {
    let validateNewPassword = (rule, value, callback) => {
      if (value === this.form.oldPassword) {
        callback(new Error("新密码不能与原密码相同!"));
      } else {
        callback();
      }
    };
    let validateNewPassword2 = (rule, value, callback) => {
      if (value !== this.form.newPassword) {
        callback(new Error("与新密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {
        enterpriseAccountNumber: "",
        oldPassword: "",
        newPassword: "",
        newPassword2: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "请设置新密码", trigger: "blur" },
          { validator: validateNewPassword, trigger: "blur" },
        ],
        newPassword2: [
          { required: true, message: "请确认新密码", trigger: "blur" },
          { validator: validateNewPassword2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async onSubmit() {
      await this.$http
        .post("/enterprise/updatePassword", Qs.stringify( this.form))
        .then((res) => {
            console.log(res);
            this.$router.push("/platform");
          
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style>
.el-form-item {
  margin-top: 30px;
  margin-bottom: 30px;
  margin-left: 25%;
}
.in0 {
  width: 50%;
}
</style>